//
//  TrainingTableViewController.m
//  B
//
//  Created by lanou on 16/5/17.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "TrainingTableViewController.h"
#import "TrainingModel.h"
#import "TrainingTableViewCell.h"
#import "TrainingDetailViewController.h"

@interface TrainingTableViewController ()
@property (nonatomic, strong) NSMutableArray *listArrs;
@end

@implementation TrainingTableViewController

- (NSMutableArray *)listArrs
{
    if (!_listArrs) {
        _listArrs = [NSMutableArray array];
    }
    return _listArrs;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.title = self.string;
    [self requestData];
    [self.tableView registerNib:[UINib nibWithNibName:@"TrainingTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

- (void)requestData
{
    NSArray *array = @[@"http://fit.jianmeile.cn:8001/study/actions?apiKey=dff83d803c8fcea96b7122785182ac24&apiSign=1e6953a8fb7d7b868b048df0bcc344cb&firstId=1&secondId=1&t=1463471389&thirdId=1", @"http://fit.jianmeile.cn:8001/study/actions?apiKey=dff83d803c8fcea96b7122785182ac24&apiSign=a5f7149cdae1d86869231f004d07f7c5&firstId=1&secondId=2&t=1463471392&thirdId=2", @"http://fit.jianmeile.cn:8001/study/actions?apiKey=dff83d803c8fcea96b7122785182ac24&apiSign=0699535cbbc90a79e9cf58c11acb6779&firstId=1&secondId=3&t=1463471396&thirdId=3", @"http://fit.jianmeile.cn:8001/study/actions?apiKey=dff83d803c8fcea96b7122785182ac24&apiSign=a6de594accef94fd1e7c203eca1cbd29&firstId=1&secondId=4&t=1463471398&thirdId=4", @"http://fit.jianmeile.cn:8001/study/actions?apiKey=dff83d803c8fcea96b7122785182ac24&apiSign=cdeebc07d39b19e68726c5779b1e0b3e&firstId=1&secondId=5&t=1463472281&thirdId=5", @"http://fit.jianmeile.cn:8001/study/actions?apiKey=dff83d803c8fcea96b7122785182ac24&apiSign=c65d2ecb808bb240b69a8abf29c05be9&firstId=1&secondId=6&t=1463472302&thirdId=6"];
    AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];
    [manage GET:array[self.index] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"];
        for (NSDictionary *dic in arr) {
            TrainingModel *mode = [[TrainingModel alloc] init];
            [mode setValuesForKeysWithDictionary:dic];
            [self.listArrs addObject:mode];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArrs.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 105;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TrainingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    TrainingModel *model = self.listArrs[indexPath.row];
    cell.nameLable.text = model.title;
    cell.contentLable.text = model.trainingBodyParts;
    [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.coverURL]];
    // Configure the cell...
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TrainingDetailViewController *detail = [[TrainingDetailViewController alloc] init];
    TrainingModel *model = self.listArrs[indexPath.row];
    detail.training = model;
    [self.navigationController pushViewController:detail animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
